<?php

namespace common\helpers;

use Yii;

/**
 * 日期时间扩展操作函数
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class DateHelper {

    /**
     * 转化时间未ISO8601 UTC
     * @param integer $timestamp 时间戳
     * @return string
     */
    public static function gmtToISO8601($timestamp) {
        $dtStr = date("c", $timestamp);
        $mydatetime = new \DateTime($dtStr);
        $expiration = $mydatetime->format(\DateTime::ISO8601);
        $pos = strpos($expiration, '+');
        return substr($expiration, 0, $pos) . "Z";
    }

    /**
     * 获取一年内星期列表、每周起始时间
     *
     * @param string $format 日期时间格式
     * @param string $year 年份
     * @return array 一年内的所有星期的起止时间
     */
    public static function getWeeksOfYear($format = "timestamp", $year = null) {
        if ($year == null) {
            $year = date('Y');
        }
        $second_of_day = 24 * 3600;
        $year_start = $year . "-01-01 00:00:00";
        $year_end = $year . "-12-31 23:59:59";
        $startday = strtotime($year_start);
        $first_week_start = $startday;
        $first_week_end = $startday + (7 - date('N', $startday)) * $second_of_day + $second_of_day - 1;
        $week_array[0] = array($first_week_start, $first_week_end);

        $endday = strtotime($year_end);
        $last_week_start = $endday - date('N', $endday) * $second_of_day + 1;
        $last_week_end = $endday;
        $last_week = array($last_week_start, $last_week_end);

        $during = $last_week_start - $first_week_end;

        $during_total_week = intval($during / (7 * $second_of_day));

        for ($i = 0; $i < $during_total_week; $i++) {
            $week_array[$i + 1] = array($first_week_end + (7 * $i * $second_of_day) + 1, $first_week_end + (($i + 1) * $second_of_day * 7));
        }

        $week_array[$during_total_week + 1] = $last_week;

        if ($format != "timestamp") {
            foreach ($week_array as $i => $week) {
                $week_array_of_date_format[$i] = [
                    date($format, $week[0]),
                    date($format, $week[1]),
                ];
            }
            return $week_array_of_date_format;
        }
        return $week_array;
    }

    /**
     * 获取时间间隔的区间
     *
     * @param mixed $format 时间戳或者是关键字字符串
     * @param int $duration 时间间隔（单位天）
     * @return array 起止时间数组
     */
    public static function timeInterval($format = "today", $duration = 1) {
        $begintime = false;
        $endtime = false;
        if ($format == 'today') {
            $begintime = strtotime('today');
            $endtime = $begintime + 86400 - 1;
        } elseif ($format == 'tomorrow') {
            $begintime = strtotime('tomorrow');
            $endtime = $begintime + 86400 - 1;
        } elseif ($format == 'weekend') {
            $begintime = strtotime('this week Saturday');
            $endtime = $begintime + 2 * 86400 - 1;
        } elseif ($format == 'week') {
            $begintime = strtotime('this week Monday', time());
            $endtime = $begintime + 7 * 86400 - 1;
        } elseif ($format == 'month') {
            $begintime = strtotime('first day of this month', strtotime('today'));
            $endtime = strtotime('last day of this month', strtotime('tomorrow') - 1);
        } elseif (is_int($format)) {
            $begintime = $format;
            $endtime = $begintime + $duration * 86400 - 1;
        }

        if ($begintime === false || $endtime === false) {
            return false;
        }

        return compact('begintime', 'endtime');
    }

    /**
     * 转化时间未ISO8601 UTC
     * @param integer $timestamp 时间戳
     * @return string
     */
    public static function gmtToISO8601($timestamp) {
        $dtStr = date("c", $timestamp);
        $mydatetime = new \DateTime($dtStr);
        $expiration = $mydatetime->format(\DateTime::ISO8601);
        $pos = strpos($expiration, '+');
        return substr($expiration, 0, $pos) . "Z";
    }

}
